package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglConstructPlanAmiYcMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglConstructPlanAmiYc;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglLicenseProcessing;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglConstructPlanAmiYcService;
import org.seimicrawler.xpath.JXNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglConstructPlanAmiYcServiceImpl implements TTgyglConstructPlanAmiYcService {

    private Logger log = LoggerFactory.getLogger(TTgyglConstructPlanAmiYcServiceImpl.class);

    @Autowired
    private TTgyglConstructPlanAmiYcMapper tTgyglConstructPlanAmiYcMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tTgyglConstructPlanAmiYcMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TTgyglConstructPlanAmiYc record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglConstructPlanAmiYc record) {
        return 0;
    }

    @Override
    public TTgyglConstructPlanAmiYc selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglConstructPlanAmiYc record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglConstructPlanAmiYc record) {
        return 0;
    }

    @Override
    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        //先删除对应的数据
        tTgyglConstructPlanAmiYcMapper.deleteByNfAndFf(year, month);

        List<JXNode> trNodeList = jxNode.sel("//tr");

        TTgyglConstructPlanAmiYc tTgyglConstructPlanAmiYc;
        //标包名称
        String packageName = "";

        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            //A表包 在第1行
            if (index == 1) {
                packageName = getPackageName(year, id, month, item);
            }

            //B表包 在第21行
            if (index == 21) {
                packageName = getPackageName(year, id, month, item);
            }
            //C表包 在第28行
            if (index == 28) {
                packageName = getPackageName(year, id, month, item);
            }
            //D表包 在第37行
            if (index == 37) {
                packageName = getPackageName(year, id, month, item);
            }
            //D表包 在第46行
            if (index == 46) {
                packageName = getPackageName(year, id, month, item);
            }
            //D表包 在第47行
            if (index == 47) {
                packageName = getPackageName(year, id, month, item);
            }
            //D表包 在第49行
            if (index == 49) {
                packageName = getPackageName(year, id, month, item);
            }//D表包 在第51行
            if (index == 51) {
                packageName = getPackageName(year, id, month, item);
            }
            //从D表包开始往下
            if (index != 0 && index != 1 && index != 21 && index != 28 && index != 37
                    && index != 46 && index != 47 && index != 49 && index != 51) {
                List<JXNode> tdNodeList = item.sel("//td");
                tTgyglConstructPlanAmiYc = new TTgyglConstructPlanAmiYc();

                //年
                tTgyglConstructPlanAmiYc.setYear(year);
                //月
                tTgyglConstructPlanAmiYc.setMonth(month);
                //文件id
                tTgyglConstructPlanAmiYc.setFileId(id);
                //标包名
                tTgyglConstructPlanAmiYc.setPackageName(packageName);
                //主要内容
                tTgyglConstructPlanAmiYc.setContent(
                        tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString());
                //进度目标
                tTgyglConstructPlanAmiYc.setPlanAim(
                        tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());
                //实际进度
                tTgyglConstructPlanAmiYc.setRemarks(
                        tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
                //保存数据
                tTgyglConstructPlanAmiYcMapper.insert(tTgyglConstructPlanAmiYc);
            }
        }
    }

    //获取报名并且保存数据
    private String getPackageName(int year,int id, int month, JXNode item) {
        TTgyglConstructPlanAmiYc tTgyglConstructPlanAmiYc;
        String packageName;
        List<JXNode> tdNodeList = item.sel("//td");
        tTgyglConstructPlanAmiYc = new TTgyglConstructPlanAmiYc();

        //年
        tTgyglConstructPlanAmiYc.setYear(year);
        //月
        tTgyglConstructPlanAmiYc.setMonth(month);
        //文件id
        tTgyglConstructPlanAmiYc.setFileId(id);
        //标包A
        packageName = tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString();
        //标包名
        tTgyglConstructPlanAmiYc.setPackageName(packageName);
        //计划进度
        tTgyglConstructPlanAmiYc.setContent(
                tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());
        //实际进度
        tTgyglConstructPlanAmiYc.setPlanAim(
                tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
        //实际进度
        tTgyglConstructPlanAmiYc.setRemarks(
                tdNodeList.get(4).selOne("//p//text()") == null ? "" : tdNodeList.get(4).selOne("//p//text()").toString());
        //保存数据
        tTgyglConstructPlanAmiYcMapper.insert(tTgyglConstructPlanAmiYc);
        return packageName;
    }

    @Override
    public List<TTgyglConstructPlanAmiYc> selectListByFileId(Integer fileId) {
        List<TTgyglConstructPlanAmiYc> resulList = null;
        TTgyglConstructPlanAmiYc tTgyglConstructPlanAmiYc = new TTgyglConstructPlanAmiYc();
        tTgyglConstructPlanAmiYc.setFileId(fileId);
        try {
            resulList = tTgyglConstructPlanAmiYcMapper.selectByBean(tTgyglConstructPlanAmiYc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
